#  This file is part of FlickFleck.
#  Copyright (C) 2008 Jyke Tapani Jokinen
#
#    FlickFleck is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FlickFleck is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FlickFleck.  If not, see <http://www.gnu.org/licenses/>.
#
import sys
import Tkinter, tkFileDialog
import Pmw
import aboutwin
import config
import configuration
import ui
import i18n
import progressbar

class FlickFleck:
    # create all windows when object is made
    def __init__(self, parent):
        self.parent = parent
	self.parent.protocol("WM_DELETE_WINDOW",self.doquit)
        self.parent.bind('<Return>', self._processReturnKey)
	self.parent.focus_set()

        self.w_about = aboutwin.About(parent)

        self.balloon = Pmw.Balloon( parent )

        # --- top menubar ---
        self.menubar = Pmw.MenuBar( parent,
                                    hull_relief = 'raised',
                                    hull_borderwidth = 1,
                                    balloon = self.balloon )
        self.menubar.pack( fill = 'x' )

        self.menubar.addmenu('File', None, text = i18n.name['menu_file'] )
        self.menubar.addmenuitem('File', 'command', None,
                             command = self.doSelFromdir,
                             label = i18n.name['menu_file_fromdir'] )
        self.menubar.addmenuitem('File', 'command', None,
                             command = self.doSelTodir,
                             label = i18n.name['menu_file_todir'] )
        if sys.platform == 'darwin':
            self.menubar.addmenuitem('File', 'separator')
            self.menubar.addmenuitem('File', 'command', None,
                                     command = configuration.mac_editconfig,
                                     label = i18n.name['menu_file_config'] )
        self.menubar.addmenuitem('File', 'separator')            
        self.menubar.addmenuitem('File', 'command', None,
                                 command = self.doquit,
                                 label =i18n.name['menu_file_exit'] )
        
        self.button_about = Tkinter.Button( self.menubar.component('hull'),
                                            text = i18n.name['menu_about'] +
                                            '...', relief = 'flat',
                                            command = self.about )
        self.button_about.pack(side='right')
        # self.balloon.bind( self.button_about, 'About')

        # --- from directory ---
        self.w_from = Pmw.Group( parent,
                                 tag_text = i18n.name['label_copyfrom'] )
        self.w_from.pack( fill = 'both', expand = 1, padx = 6, pady = 6 )

        self.var_fromlabel = Tkinter.StringVar( value = config.fromdir )
        self.w_fromlabel = Tkinter.Label( self.w_from.interior(),
                                          textvariable = self.var_fromlabel,
                                          pady = 10 )
        self.w_fromlabel.pack( side = 'left', padx = 5, pady = 2 )

        # --- to directory ---
        self.w_to = Pmw.Group( parent, tag_text = i18n.name['label_copyto'] )
        self.w_to.pack( fill = 'both', expand = 1, padx = 6, pady = 6 )

        self.var_tolabel = Tkinter.StringVar( value = config.todir )        
        self.w_tolabel = Tkinter.Label( self.w_to.interior(),
                                        textvariable = self.var_tolabel )
        self.w_tolabel.pack( side = 'left', padx = 5, pady = 2 )

        # --- start n stop buttons ---
        self.w_buttons = Pmw.ButtonBox( parent )
#                                        frame_borderwidth = 2,
#                                        frame_relief = 'groove' )
        self.w_buttons.pack( fill = 'both', expand = 1, padx = 10, pady = 10 )
        self.w_buttons.add( 'Start', command = self.start,
                            text = i18n.name['button_start'] )
        self.w_buttons.add( 'Stop', command = self.stop,
                            text = i18n.name['button_stop'] )
        self.w_buttons.setdefault('Start')
        self.w_buttons.alignbuttons()

        self.w_buttons.button('Stop').config(state = Tkinter.DISABLED)
        self.w_buttons.setdefault('Start')

        # --- progressbar ---
#        self.w_pb = Pmw.LabeledWidget( parent, labelpos = "nw",
#                                       label_text = "" )
        self.w_pb = Pmw.ScrolledCanvas( parent,
                                        borderframe = 0,
                                        usehullsize = 1,
                                        hscrollmode = 'none',
                                        vscrollmode = 'none',
                                        hull_height = ui.wins.PBheight )

        self.w_pb.component('hull').configure(relief='sunken', borderwidth=2)
        self.w_pb.pack( padx=5, pady=5, expand='yes', fill = 'x')
        self.w_pbw = progressbar.Widget( self.w_pb.interior() )
        ui.wins.pb = self.w_pbw

        # --- status area at the bottom ---
        self.w_status = Pmw.MessageBar( parent, entry_relief = 'groove',
                                      labelpos = 'w',
                                      label_text =
                                      i18n.name['label_status'] + ' ')
        self.w_status.silent = True
        self.w_status.message('state', i18n.name['status_idle'] )
        self.w_status.pack( fill = 'x', padx = 2, pady = 1, side = 'bottom' )
#        self.balloon.configure( statuscommand = self.w_status.helpmessage )

    #-- set message in the statusrow
    def status(self, msg_str, type = 'state'):
        self.w_status.message(type, msg_str)
        ui.wins.root.update()

    #-- user pressed 'start'
    def start(self):
        self.w_buttons.button(1).config(state = Tkinter.NORMAL)
        self.w_buttons.button(0).config(state = Tkinter.DISABLED)
        ui.wins.requestStop = False
        ui.wins.engine.start()

    #-- user pressed 'cancel'
    def stop(self):
        # self.w_buttons.button(0).config(state = Tkinter.NORMAL)
        self.w_buttons.button(1).config(state = Tkinter.DISABLED)
        # stopping is only noted, engine will stop when it notices value change
        ui.wins.requestStop = True

    #-- user pressed Enter
    def _processReturnKey(self, event):
        self.w_buttons.invoke()

    #-- quit selected in file-menu
    def doquit(self):
	self.parent.quit()

    #-- about button pressed
    def about(self):
        return self.w_about.show()
        
    #-- select fromdir
    def doSelFromdir(self):
        dir = tkFileDialog.askdirectory()
        if len(dir) > 0:
            config.fromdir = dir        
            self.var_fromlabel.set( dir )
            ui.wins.engine.init_filelist() # refind jpgs to copy


    #-- select todir
    def doSelTodir(self):
        dir = tkFileDialog.askdirectory()
        if len(dir) > 0:
            config.todir = dir        
            self.var_tolabel.set( dir )
        
